package com.qimo.controller;

import com.alibaba.fastjson.JSONObject;
import com.qimo.model.*;
import com.qimo.service.ShopService;
import com.qimo.service.UserService;
import com.qimo.tool.ResultInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class ShopController {
    @Resource
    private ShopService shopService;
    @Resource
    private UserService userService;

    @ResponseBody
    @RequestMapping("/shop/shop")
    public ResultInfo shopHtml(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入shopHtml");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int shopId=Integer.parseInt(request.getParameter("shopId"));
        System.out.println(shopId);
        Shop shop =this.shopService.Shop(shopId);
        if(null==shop)//无结果
        {
            System.out.println("ShopController出错");
            return ResultInfo.failure("查无此店,有点奇怪的");
        }
        else {
            System.out.println(shop);
            ResultInfo resultInfo=new ResultInfo();
            resultInfo.setCode(1);
            resultInfo.setMsg("查询成功");
            resultInfo.setResult(shop);
            return resultInfo;

        }
    }

    @ResponseBody
    @RequestMapping("/shop/shopApplication")
    public ResultInfo shopApplication(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入shopHtml");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int userId=Integer.parseInt(request.getParameter("userId"));
        String userName = request.getParameter("userName");
        String passWord = request.getParameter("passWord");
        String shopName = request.getParameter("shopName");
        if(shopName==null)
            return ResultInfo.failure("店铺不能为空 非正常操作");
        User temp=userService.selectUser(userName);
        if(temp.getPassWord().equals(passWord)&&temp.getUserId()==userId)
        {
            if(this.shopService.shopApplication(userId,shopName)>0)
            return ResultInfo.success();
        else return ResultInfo.failure("多半已经有店铺了");
        }
        else return ResultInfo.failure("用户信息有误");

    }
    @ResponseBody
    @RequestMapping("/shop/shopOrderSelect")
    public ResultInfo shopOrderSelect(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入shopOrderSelect");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int userId=Integer.parseInt(request.getParameter("userId"));
        int Page=Integer.parseInt(request.getParameter("Page"));
        int limit=Integer.parseInt(request.getParameter("limit"));
        String userName = request.getParameter("userName");
        String passWord = request.getParameter("passWord");
        System.out.println(userId);
        System.out.println(Page);
        System.out.println(userName);
        System.out.println(passWord);
        User temp=this.userService.selectUser(userName);
        if(!(temp.getUserId()==userId&&temp.getPassWord().equals(passWord)))
        return ResultInfo.failure("用户信息不对称");
        if(this.shopService.checkHaveShop(userId)==null)
            return ResultInfo.failure("无店铺信息");
        ResultInfo resultInfo= ResultInfo.success();
        resultInfo.setCode(0);
        resultInfo.setResult(this.shopService.shopOrderSelect(userId,Page,limit));
        resultInfo.setCount(this.shopService.shopOrderSelectCount(userId));
        return resultInfo;

    }
    @ResponseBody
    @RequestMapping("/shop/selectBook")
    public ResultInfo selectBook(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int page=Integer.parseInt(request.getParameter("page"));
        int limit=Integer.parseInt(request.getParameter("limit"));

        System.out.println(page);
        System.out.println(limit);

        ResultInfo resultInfo= ResultInfo.success();
        resultInfo.setCode(0);
        resultInfo.setResult(this.shopService.selectBook(page,limit));
        resultInfo.setCount(this.shopService.selectBookCount());
        return resultInfo;

    }
    @ResponseBody
    @RequestMapping("/shop/createItem")
    public ResultInfo createItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入createItem");
        int userId=Integer.parseInt(request.getParameter("userId"));
        int itemNumber=Integer.parseInt(request.getParameter("itemNumber"));
        double itemPrice=Double.parseDouble(request.getParameter("itemPrice"));
        String userName = request.getParameter("userName");
        String passWord = request.getParameter("passWord");
        String itemName = request.getParameter("itemName");
        String itemContent = request.getParameter("itemContent");
        String itemImage = request.getParameter("itemImage");
        List<Book> book= JSONObject.parseArray(request.getParameter("book"),Book.class);
        if(book.isEmpty()||book==null)
            return ResultInfo.failure("不要擅自改代码啊");


        int shopId=this.shopService.checkHaveShop(userId).getShopId();
        CreateItem createItem=new CreateItem();
        createItem.setItemContent(itemContent);
        createItem.setItemImage(itemImage);
        createItem.setItemName(itemName);
        createItem.setItemPrice(itemPrice);
        createItem.setItemNumber(itemNumber);
        createItem.setShopId(shopId);
    int itemId=this.shopService.createItem(createItem);
    System.out.println(itemId);
        this.shopService.create_book_item(userId,itemId,book,shopId);

return ResultInfo.success();
    }

    @ResponseBody
    @RequestMapping("/shop/fahuo")
    public ResultInfo fahuo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入shopOrderSelect");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int userId=Integer.parseInt(request.getParameter("userId"));
        String userName = request.getParameter("userName");
        String passWord = request.getParameter("passWord");

        System.out.println(userId);
        System.out.println(userName);
        System.out.println(passWord);
        User temp=this.userService.selectUser(userName);
        if(!(temp.getUserId()==userId&&temp.getPassWord().equals(passWord)))
            return ResultInfo.failure("用户信息不对称");
        ResultInfo resultInfo= ResultInfo.success();
        resultInfo.setCount(this.shopService.fahuo(userId));
        resultInfo.setCode(1);
        return resultInfo;

    }
}
